# coding = utf-8
import os
import openpyxl
from shutil import copyfile
from openpyxl.styles import Font, PatternFill, Border, Side, Alignment, Protection
from Common.Excel import newExcel, oldExcel


def get_reader(scr_file=''):
    """
    读取excel 文件
    :param scr_file: Excel 文件路径
    :return:
    """
    reader = None

    # 如果打开文件路径不存在就报错
    if not os.path.isfile(scr_file):
        print('%s not exist!' % (scr_file))
        return reader

    if scr_file.endswith('.xls'):
        reader = oldExcel.Reader()
        reader.open_excel(scr_file)
        return reader

    if scr_file.endswith('.xlsx'):
        reader = newExcel.Reader()
        reader.open_excel(scr_file)
        return reader


def get_writer(scr_file, dst_file):
    """
    后去写入excel文件
    :param scr_file: excel的文件路径
    :param dst_file:
    :return:
    """
    writer = None

    if not os.path.isfile(scr_file):
        print('%s is not exist' % (scr_file))
        return writer

    if scr_file.endswith('.xls'):
        writer = oldExcel.Writer()
        writer.copy_open(scr_file, dst_file)
        return writer

    if scr_file.endswith('.xlsx'):
        writer = newExcel.Writer()
        writer.copy_open(scr_file, dst_file)
        return writer


if __name__ == "__main__":

    reader = get_reader('../../lib/web/电商项目用例.xlsx')
    # 获取sheet页
    sheetname = reader.get_sheets()
    # print(sheetname)
    # 遍历sheet页面
    for sheet in sheetname:
        cases = []
        case = []
        # 设置读取的sheet页
        reader.set_sheet(sheet)
        # 获取所有行,处理成二维列表
        lines = reader.readline()
        # print("获取所有行,处理成二维列表:"+str(lines))
        # 遍历行
        for i in range(len(lines)):
            line=lines[i]
            # print('第'+str(i)+"内容"+str(line))
            if len(line[0])>0:
                print(line[0])
                if case:
                    cases.append(case)
                    case = []
                pass
            elif len(line[1])>0:
                if case:
                    cases.append(case)
                    case=[]
                case.append(line)
            else:
                case.append(line)
            # print('二维列表')
            # print(case)
        print()
        print(sheet)
        print(cases)
            # print('第'+str(i)+'内容line[0]:'+line[0])
            # print('第'+str(i)+'内容line[0]:的长度：'+str(len(line[0])))
            # print('第'+str(i)+'内容line[1]:'+line[1])
            # print('第'+str(i)+'内容line[1]:的长度：'+str(len(line[1])))
        # print()

    # writer = get_writer('../../lib/电商项目用例test.xlsx', '../../lib/result_电商项目用例.xlsx')
    # sheetname = writer.get_sheets()
    # writer.set_sheet(sheetname[0])
    # writer.write(3, 7, 'pass', 3)
    # writer.save_close()
